package com.qian.ddj.web.controller;

import com.qian.api.ddj.util.FileUtil;
import com.yaowk.api.common.BaseException;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/ddj/upload")
@Api(tags = "文件上传")
public class UploadController {
    @PostMapping("/save")
    public String save(@RequestParam("file") MultipartFile file,HttpServletRequest request){
        String contentType = file.getContentType();
        String fileName = file.getOriginalFilename();

        String filePath = request.getSession().getServletContext().getRealPath("alilpayQrcode/");
        try {
            String resultPath = FileUtil.uploadFile(file.getBytes(),filePath,fileName);
            return resultPath;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException("上传文件出错");
        }
    }
}
